using DevExpress.Blazor;
using EasyXaf.LowCode.WorkflowEditors.Models;
using EasyXaf.LowCode.WorkflowEditors.UndoManagers;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace EasyXaf.LowCode.WorkflowEditors.Nodes;

public partial class NodeTitleComponent
{
    private DxMemo CaptionMemo { get; set; }

    private string CaptionValue { get; set; }

    private bool CaptionEditing { get; set; }

    private string NodeComponentClassName
    {
        get
        {
            var classNames = new List<string>
            {
                "node-title-component"
            };

            if (Node.HasDetailView)
            {
                classNames.Add("node-has-detail-view");
            }

            return string.Join(" ", classNames);
        }
    }

    private string NodeComponentStyle
    {
        get
        {
            var styles = new List<string>
            {
                $"--wf-node-border-color:{Node.Color}",
                $"--wf-node-scale:{Node.TitleViewScale}",
            };
            if (MiniMode)
            {
                styles.Add("--wf-node-width:80px");
                styles.Add($"--wf-node-height:calc(var(--wf-node-min-height) / 2)");
            }
            else
            {
                styles.Add("--wf-node-height:var(--wf-node-min-height)");
            }
            return string.Join(";", styles);
        }
    }

    private bool IsReadOnly
    {
        get => WorkflowMode >= WorkflowMode.Debug;
    }

    [CascadingParameter]
    public IUndoManager UndoManager { get; set; }

    [CascadingParameter]
    public WorkflowMode WorkflowMode { get; set; }

    [Parameter]
    public bool MiniMode { get; set; }

    [Parameter]
    public INode Node { get; set; }

    [Parameter]
    public EventCallback EnterContainer { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> ShowNodeMenu { get; set; }

    private void ProcessMemoKeyDown(KeyboardEventArgs e)
    {
        if (e.Code == "Escape")
        {
            if (CaptionEditing)
            {
                CancelEditCaption();
            }
        }
        else if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            if (CaptionEditing)
            {
                EndEditCaption();
            }
        }
    }

    private void BeginEditCaption()
    {
        if (!IsReadOnly)
        {
            CaptionValue = Node.Caption;
            CaptionEditing = true;
        }
    }

    private void CancelEditCaption()
    {
        CaptionEditing = false;
    }

    private void EndEditCaption()
    {
        if (!string.IsNullOrWhiteSpace(CaptionValue))
        {
            var caption = CaptionValue.Replace("\r\n", string.Empty).Replace("\n", string.Empty);
            if (Node.Caption != caption)
            {
                UndoManager.ChangeNodeCaption(Node, caption);
            }
        }
        CaptionEditing = false;
    }

    private void ChangeViewMode()
    {
        if (Node.HasDetailView)
        {
            UndoManager.ChangeNodeViewMode(Node, NodeViewMode.Detail);
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (CaptionEditing && CaptionMemo != null)
        {
            await CaptionMemo.FocusAsync();
        }
    }
}